package com.vincce.sport.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.vincce.sport.core.utils.ConvertUtil;
import com.vincce.sport.core.utils.SimpleStorageClient;
import com.vincce.sport.entity.CompetitionSeason;
import com.vincce.sport.entity.ImgStorage;
import com.vincce.sport.entity.Page;
import com.vincce.sport.entity.PersonalRankView;
import com.vincce.sport.entity.School;
import com.vincce.sport.entity.SchoolRankDataView;
import com.vincce.sport.entity.SchoolRankView;
import com.vincce.sport.entity.Zone;
import com.vincce.sport.mapper.CompetitionSeasonMapper;
import com.vincce.sport.mapper.PersonalRankViewMapper;
import com.vincce.sport.mapper.SchoolMapper;
import com.vincce.sport.mapper.SchoolRankDataViewMapper;
import com.vincce.sport.mapper.SchoolRankViewMapper;
import com.vincce.sport.mapper.ZoneMapper;

@Controller
@RequestMapping("/ranking")
public class RankingController {
	
	@Resource
	private ZoneMapper zoneMapper;
	@Resource
	private CompetitionSeasonMapper competitionSeasonMapper;
	@Resource
	private SchoolMapper schoolMapper;
	@Resource
	private PersonalRankViewMapper personalRankViewMapper;
	@Resource
	private SchoolRankDataViewMapper schoolRankDataViewMapper;
	@Resource
	private SchoolRankViewMapper schoolRankViewMapper;
	//------------------------------前台排名----------------------------//
	/**
	 * 个人-校内排名
	 * @param model
	 * @param seasonId
	 * @param zoneId
	 * @param request
	 * @return
	 */
	@RequestMapping("/personalRankCampusList")
    public String personalRankCampusList(Model model, Integer type,Long schoolId, HttpServletRequest request){			
		//获取当前赛季
		Long seasonId=getCompetitionSeasonId();
		String schoolName="";
		if(type==null){
			type=1;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("seasonId", seasonId);
		List<School> schoolList=schoolMapper.querySchoolBySeasonId(map);
		if(schoolId==null){			
			if(schoolList!=null && schoolList.size()>0){					
				schoolId=schoolList.get(0).getId().longValue();
				schoolName=schoolList.get(0).getName();
			}
		}
		Map<String, Object> filter = new HashMap<String, Object>();	
		Page<PersonalRankView> page=new Page<PersonalRankView>();
		filter=new HashMap<String, Object>();	
	    filter.put("type", type);
	    filter.put("schoolId", schoolId);
	    filter.put("seasonId", seasonId);
	    page.setLimit(50);
	    page.setFilter(filter);
	    page.setResult(replace(personalRankViewMapper.queryPersonalRankCampusViewList(page)));
	    model.addAttribute("personalRankCampusList",page);	
		model.addAttribute("schoolList",schoolList);	
		model.addAttribute("type",type);	
		model.addAttribute("schoolId",schoolId);
		model.addAttribute("schoolName",schoolName);
		String updateTime="";
		if(page!=null && page.getResult().size()>0){
			updateTime=page.getResult().get(0).getUpdateTime();
			if(updateTime==""){
				updateTime=ConvertUtil.format("yyyy-MM-dd", new Date());
			}
		}else{
			updateTime=ConvertUtil.format("yyyy-MM-dd", new Date());
		}		
		model.addAttribute("updateTime",updateTime);
        return "foreground/personalRankCampus";
    }
	/**
	 * 个人-联盟排名
	 * @param model
	 * @param seasonId
	 * @param zoneId
	 * @param request
	 * @return
	 */
	@RequestMapping("/personalRankLeagueList")
    public String personalRankLeagueList(Model model, Integer type,HttpServletRequest request){
		//获取当前赛季
		Long seasonId=getCompetitionSeasonId();
		if(type==null){
			type=1;
		}		
		Map<String, Object> filter = new HashMap<String, Object>();	
		Page<PersonalRankView> page=new Page<PersonalRankView>();
		filter=new HashMap<String, Object>();	
	    filter.put("type", type);
	    filter.put("seasonId", seasonId);
	    page.setLimit(50);
	    page.setFilter(filter);
	    page.setResult(replace(personalRankViewMapper.queryPersonalRankLeagueViewList(page)));
	    model.addAttribute("personalRankLeagueList",page);		
		model.addAttribute("type",type);
		String updateTime="";
		if(page!=null && page.getResult().size()>0){
			updateTime=page.getResult().get(0).getUpdateTime();
			if(updateTime==""){
				updateTime=ConvertUtil.format("yyyy-MM-dd", new Date());
			}
		}else{
			updateTime=ConvertUtil.format("yyyy-MM-dd", new Date());
		}
		model.addAttribute("updateTime",updateTime);
        return "foreground/personalRankLeague";
    }
	/**
	 * 学校排名
	 * @param model
	 * @param seasonId
	 * @param zoneId
	 * @param request
	 * @return
	 */
	@RequestMapping("/schoolWebList")
    public String newsWebDetail(Model model, Long seasonId,Long zoneId, HttpServletRequest request){
		if(seasonId==null){
			Map<String, Object> filter = new HashMap<String, Object>();	
			Page<CompetitionSeason> page=new Page<CompetitionSeason>();
			filter=new HashMap<String, Object>();	
		    filter.put("status", 1);
		    page.setLimit(1);
		    page.setFilter(filter);
		    page.setResult(competitionSeasonMapper.findCompetitionSeasonList(page));
			if(page!=null && page.getResult().size()>0){	
				seasonId=page.getResult().get(0).getId();
			}
		}
		if(zoneId==null){
			Map<String, Object> filter = new HashMap<String, Object>();	
			Page<Zone> page=new Page<Zone>();
			filter=new HashMap<String, Object>();	
		    filter.put("status", 1);
		    page.setLimit(1);
		    page.setFilter(filter);
		    page.setResult(zoneMapper.queryByZonePage(page));
			if(page!=null && page.getResult().size()>0){	
				zoneId=page.getResult().get(0).getId().longValue();
			}
		}
		List<SchoolRankView> schoolRankViewList=new ArrayList<SchoolRankView>();
		SchoolRankView schoolRankView=new SchoolRankView();
		schoolRankView.setSeasonId(seasonId);
		schoolRankView.setZoneId(zoneId);
		SchoolRankDataView schoolRankDataView=new SchoolRankDataView();
		schoolRankDataView.setSeasonId(seasonId);
		schoolRankDataView.setZoneId(zoneId);
		List<SchoolRankDataView> schoolRankDataViewList=schoolRankDataViewMapper.querySchoolRankDataView(schoolRankDataView);
		if(schoolRankDataViewList!=null && schoolRankDataViewList.size()>0){
			for(SchoolRankDataView schoolRankDataView1:schoolRankDataViewList){
				schoolRankView.setSchoolId(schoolRankDataView1.getId().longValue());
				schoolRankViewList=schoolRankViewMapper.getSchoolRankViewList(schoolRankView);
				if(schoolRankViewList!=null && schoolRankViewList.size()>0){
					int i=1;
					for(SchoolRankView schoolRankView1:schoolRankViewList){
						schoolRankView1.setId(i);
						i++;
					}
					schoolRankDataView1.setSchoolRankViewList(schoolRankViewList);
				}else{
					schoolRankDataView1.setSchoolRankViewList(new ArrayList<SchoolRankView>());
				}
			}
		}else{
			schoolRankDataViewList=new ArrayList<SchoolRankDataView>(); 
		}
		model.addAttribute("schoolRankDataViewList",schoolRankDataViewList);	 
		model.addAttribute("competitionSeasonList",competitionSeasonMapper.findCompetitionSeasonListAll());	 
		model.addAttribute("zoneList",zoneMapper.queryByZoneAll());	
		model.addAttribute("seasonId",seasonId);	
		model.addAttribute("zoneId",zoneId);	
        return "foreground/schoolRank";
    }
	
	public Long  getCompetitionSeasonId(){
		Long seasonId=null;
		Map<String, Object> filter = new HashMap<String, Object>();	
		Page<CompetitionSeason> page=new Page<CompetitionSeason>();
		filter=new HashMap<String, Object>();	
	    filter.put("status", 1);
	    page.setLimit(1);
	    page.setFilter(filter);
	    page.setResult(competitionSeasonMapper.findCompetitionSeasonList(page));
		if(page!=null && page.getResult().size()>0){				
			seasonId=page.getResult().get(0).getId();
		}
		return seasonId;
	}
	
	/**
	 * 图片路径替换
	 * @param imgStorageList
	 * @return
	 */
	public List<PersonalRankView> replace(List<PersonalRankView> imgStorageList){
		if(imgStorageList!=null && imgStorageList.size()>0){
			for(PersonalRankView isl:imgStorageList){
				isl.setPlayerUrl(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+isl.getPlayerUrl(), "ori"));
			}
		}
		return imgStorageList;
	}
}
